package com.oocl;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * 在java中如何通过线程来启动一个js方法
 * @author JasonWu
 * @version
 */
public class ScriptEngineTest4 {
	public static void main(String[] args) throws ScriptException {
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("javascript");
		String script="var obj=new Object();obj.run=function(){print('test thread')}"; 
		engine.eval(script); 
		Object obj=engine.get("obj");//获取js中对象 
	    Invocable inv=(Invocable)engine; 
	    Runnable r=inv.getInterface(obj,Runnable.class); 
	    Thread t=new Thread(r); 
	    t.start(); 
	}

}
